import tensorflow as tf
import numpy as np

# 得到数据样本
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.4 + 2

# tf 变量
b = tf.Variable(0.)
w = tf.Variable(0.)

# 定义的函数
y = w * x_data + b

# reduce_mean 计算张量维度上元素的平均值。
loss = tf.reduce_mean(tf.square(y - y_data))

# 梯度下降优化器，优化步长为0.01
train = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
init = tf.global_variables_initializer()

with tf.Session() as session:
    session.run(init)

    for i in range(10001):
        session.run(train)
        if i % 20 == 0:
            print(session.run([w, b]))
